package com.platform.web.controller.data;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.platform.base.domain.BaseLocation;
import com.platform.base.domain.TempInfo;
import com.platform.base.domain.TempInfo;
import com.platform.base.mapper.BaseLocationMapper;
import com.platform.base.service.IBaseLocationService;
import com.platform.base.service.ITempInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.platform.common.annotation.Log;
import com.platform.common.enums.BusinessType;
import com.platform.data.domain.TempData;
import com.platform.data.service.ITempDataService;
import com.platform.common.core.controller.BaseController;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.utils.poi.ExcelUtil;
import com.platform.common.core.page.TableDataInfo;

/**
 * 温度采集数据Controller
 * 
 * @author wangqi
 * @date 2020-09-27
 */
@Controller
@RequestMapping("/data/temp")
public class TempDataController extends BaseController
{
    private String prefix = "data/temp";

    @Autowired
    private ITempDataService tempDataService;

    @Autowired
    private IBaseLocationService baseLocationService;

    @Autowired
    private ITempInfoService tempInfoService;

    @RequiresPermissions("data:temp:view")
    @GetMapping(value = { "", "/{tempId}" })
    public String data(@PathVariable(value = "tempId", required = false) String tempId, ModelMap mmap)
    {
        if(tempId==null){
            tempId = "";
        }
        mmap.put("tempId", tempId);
        mmap.put("location", new BaseLocation());
        return prefix + "/data";
    }

    /**
     * 根据地址查询阀门信息
     * @param locationId
     * @param mmap
     * @return
     */
    @RequiresPermissions("data:temp:view")
    @GetMapping("/location/{locationId}")
    public String location(@PathVariable(value = "locationId") String locationId, ModelMap mmap)
    {
        String tempIds = "";
        List<TempInfo> list = tempInfoService.list(new QueryWrapper<TempInfo>().eq("location_id", locationId));
        for(TempInfo tempInfo : list){
            if(tempIds.equals("")){
                tempIds = tempInfo.getTempId();
            }else{
                tempIds = tempIds + "," + tempInfo.getTempId();
            }
        }
        mmap.put("tempId", tempIds);
        mmap.put("location", baseLocationService.getById(locationId));
        return prefix + "/data";
    }

    /**
     * 查询温度采集数据列表
     */
    @RequiresPermissions("data:temp:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TempData tempData)
    {
        startPage();
        if(tempData.getTempId() !=null && !tempData.getTempId().equals("")){
            tempData.setLocationId(null);
        }
        List<TempData> list = tempDataService.selectTempDataList(tempData);
        return getDataTable(list);
    }

    /**
     * 导出温度采集数据列表
     */
    @RequiresPermissions("data:temp:export")
    @Log(title = "温度采集数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TempData tempData)
    {
        if(tempData.getTempId() !=null && !tempData.getTempId().equals("")){
            tempData.setLocationId(null);
        }
        List<TempData> list = tempDataService.selectTempDataList(tempData);
        ExcelUtil<TempData> util = new ExcelUtil<TempData>(TempData.class);
        return util.exportExcel(list, "温度采集数据");
    }
}
